package com.wyy.servlet.web;

import com.wyy.bean.Grade;
import com.wyy.bean.Student;
import com.wyy.service.StudentService;
import com.wyy.service.impl.GradeServiceImpl;
import com.wyy.service.impl.StudentServiceImpl;
import com.wyy.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @ClassName StudentServlet
 * @Description //TODO
 * @Author wky
 * @Date 2021/9/20 20:21
 * @Version 1.0
 **/
@WebServlet(urlPatterns = "/Educational/student/studentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("insert".equals(method)){
            insert(req, resp);
        }else if ("delete".equals(method)){
            delete(req, resp);
        }else if ("update".equals(method)){
            update(req, resp);
        }else if ("findById".equals(method)){
            findById(req, resp);
        }else{
            findList(req, resp);
        }
    }
    
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String stuNo = req.getParameter("stuNo");
        System.out.println("servlet===>"+stuNo);
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        //调用service
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        StudentServiceImpl studentService = new StudentServiceImpl();
        int i = studentService.insertStu(student);
        //转发页面
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('新增成功');location.href='/Educational/student/studentServlet'</script>");
        }else if (i==-1){
            writer.println("<script>alert('新增失败-->该学号已存在！');location.href='/Educational/student/studentServlet'</script>");
        }
        else{
            writer.println("<script>alert('新增失败');location.href='/Educational/student/studentServlet'</script>");
        }
    }
    
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String sid = req.getParameter("sid");
        StudentServiceImpl studentService = new StudentServiceImpl();
        int res = studentService.delStu(sid);
    
        //转发页面
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (res>0){
            writer.println("<script>alert('删除成功');location.href='/Educational/student/studentServlet'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/Educational/student/studentServlet'</script>");
        }
    }
    
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String sid = req.getParameter("sid");
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        System.out.println("1----"+address);
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        //调用service
        Student student = new Student();
        student.setStuId(Integer.parseInt(sid));
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        StudentServiceImpl studentService = new StudentServiceImpl();
        int update = studentService.updateStu(student);
        //跳转页面
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (update>0){
            writer.println("<script>alert('修改成功');location.href='/Educational/student/studentServlet'</script>");
        }else{
            writer.println("<script>alert('修改失败');location.href='/Educational/student/studentServlet?method=findById&sid = "+sid+"'</script>");
        }
    }
    
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String sid = req.getParameter("sid");
        //调用service
        StudentServiceImpl studentService = new StudentServiceImpl();
        Student stu = studentService.findById(Integer.parseInt(sid));
    
        GradeServiceImpl gradeService = new GradeServiceImpl();
        List<Grade> grades = gradeService.getList();
    
        //转发页面
        req.setAttribute("stu",stu);
        req.setAttribute("grades",grades);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
    
    protected void findList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数
        //1.1 模糊查询条件
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sexNo = req.getParameter("sex");
        int sex = sexNo==null||sexNo.length()==0?-1:Integer.parseInt(sexNo);
        //1.2 分页数据  页码值 每页数据
        PageUtil pageUtil = new PageUtil();
        String pageIndex = req.getParameter("pageIndex");
        int index = pageIndex == null||"".equals(pageIndex) ? 1 : Integer.parseInt(pageIndex);
        pageUtil.setPageIndex(index);
        //2.调用service
        StudentService studentService = new StudentServiceImpl();
        List<Student> students =
                studentService.getStudents(stuname,stuno,sex,index,pageUtil.getPageSize());
        //获取总条数
        int total = studentService.total(stuname, stuno, sex);
        pageUtil.setTotal(total);
        pageUtil.setList(students);
        System.out.println(pageUtil.toString());
        //3.转发
        //存储模糊查询条件
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);
        //存储页数信息
        req.setAttribute("p1",pageUtil);
    
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
